/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package components;

import java.net.*;
import java.io.*;
import SRDException.*;
import SRDThread.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ylopez
 */
public class dmon {
    private static List users = new ArrayList();;
    private static int[] count = new int[]{0,0,0};
    private static final int PORT = 43665;

    public static void main(String [] args){

        int port;
        ServerSocket serverSocket;
        boolean listening;

        port = PORT;
        serverSocket = null;
        listening = true;

        try {
            if (args[0].equals("-p") && args.length == 2) {
                port = Integer.parseInt(args[1]);
            } else {
                SRDException srde = new SRDException("Llamada a drep invalida. "
                        + "Todos los argumentos son obligatorios");
                srde.printException();
                System.exit(-1);
            }
        } catch (NumberFormatException nfe) {
            //TODO Manejar esta excepcion bien
            System.err.println("El puerto no es un numero entero.");
            nfe.printStackTrace();
            System.exit(-1);
        }

        try {
            serverSocket = new ServerSocket(port);
            System.out.println("Conectado al puerto: " + port);
        } catch (IOException e) {
            System.err.println("No se puede conectar al puerto: " + port);
            System.exit(-1);
        }
        
        SRDThread principalThread = new SRDThread(null, null, 0, null, true, null, users, "dmon", count, null, null,null);
        principalThread.start();
        try {
            while (listening){
                new SRDThread(serverSocket.accept(), null, 0, null, false, null, users, "dmon", count, null, null,null).start();
            }

            serverSocket.close();
        } catch (IOException ex) {
            System.err.println(drep.class.getName() + ": Error de E/S esperando coneccion de clientes");
        }

    }

}
